<template>
  <div>

    <el-button @click="addModel(1)">添加计件收费模板</el-button>
    <el-button @click="addModel(2)">添加称重收费模板</el-button>
    <el-button @click="addModel(3)">添加包邮收费模板</el-button>

    <el-form style="margin-top: 20px" v-for="(feeModel,index) in modelList" :key="index">
      <template v-if="feeModel.type==1">
        <el-form-item class="oneInputW">
          <el-input type="text" v-model="feeModel.name" placeholder="请输入模板名称" style="padding: 10px 0 10px 30px;display: block;width: 150px"></el-input>
          <el-input style="margin-left: 30px" v-model="feeModel.limit"></el-input> 件以内 <el-input v-model="feeModel.fee"></el-input> 元，每增加 <el-input v-model="feeModel.increase"></el-input> 件， 增加运费 <el-input v-model="feeModel.increaseFee"></el-input> 元。
          <br>
          <el-button type="text" style="padding: 30px 0 0 30px" v-if="feeModel.sonType !=1" @click="byCityBtn(index)">指定地区另算</el-button>
          <template v-if="feeModel.sonType">
            <span style="padding-left: 30px">运送到</span>
            <el-select v-model="feeModel.provIds" multiple filterable placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select> 地区：
            <br>
            <el-input style="margin-left: 30px" v-model="feeModel.sonLimit"></el-input> 件以内 <el-input v-model="feeModel.sonFee"></el-input>元，每增加 <el-input v-model="feeModel.sonIncrease"></el-input> 件，增加运费 <el-input v-model="feeModel.sonIncreaseFee"></el-input> 元。
          </template>
          <div style="text-align: right;padding: 30px 30px 20px 0">
            <el-button type="text" @click="saveModeBtn(feeModel)">保存</el-button>
            <el-button type="text" style="color: red" @click="removeModel(index)">删除此模板</el-button>
          </div>
        </el-form-item>
      </template>

      <template v-if="feeModel.type==2">
        <el-form-item class="oneInputW">
          <el-input type="text" v-model="feeModel.name" placeholder="请输入模板名称" style="padding: 10px 0 10px 30px;display: block;width: 150px"></el-input>
          <el-input style="margin-left: 30px" v-model="feeModel.limit"></el-input> g以内 <el-input v-model="feeModel.fee"></el-input> 元，每增加 <el-input v-model="feeModel.increase"></el-input> g， 增加运费 <el-input v-model="feeModel.increaseFee"></el-input> 元。
          <br>
          <el-button type="text" style="padding: 30px 0 0 30px" v-if="feeModel.sonType !=2" @click="byCityBtnTwo(index)">指定地区另算</el-button>
          <template v-if="feeModel.sonType">
            <span style="padding-left: 30px">运送到</span>
            <el-select v-model="feeModel.provIds" multiple filterable placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select> 地区：
            <br>
            <el-input style="margin-left: 30px" v-model="feeModel.sonLimit"></el-input> g以内 <el-input v-model="feeModel.sonFee"></el-input>元，每增加 <el-input v-model="feeModel.sonIncrease"></el-input> g，增加运费 <el-input v-model="feeModel.sonIncreaseFee"></el-input> 元。
          </template>
          <div style="text-align: right;padding: 30px 30px 20px 0">
            <el-button type="text" @click="saveModeBtn(feeModel)">保存</el-button>
            <el-button type="text" style="color: red" @click="removeModel(index,feeModel.type)">删除此模板</el-button>
          </div>
        </el-form-item>
      </template>

      <template v-if="feeModel.type==3">
        <el-form-item class="oneInputW">
          <el-input type="text" v-model="feeModel.name" placeholder="请输入模板名称" style="padding: 10px 0 10px 30px;display: block;width: 150px"></el-input>

          <el-button type="text" style="padding: 30px 0 0 30px" v-if="!feeModel.sonType" @click="byCityBtn(index)">指定地区计件另算</el-button>
          <el-button type="text" style="padding: 30px 0 0 30px" v-if="!feeModel.sonType" @click="byCityBtnTwo(index)">指定地区计重另算</el-button>
          <template v-if="feeModel.sonType==1">
            <span style="padding-left: 30px">运送到</span>
            <el-select v-model="feeModel.provIds" multiple filterable placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select> 地区：
            <br>
            <el-input style="margin-left: 30px" v-model="feeModel.sonLimit"></el-input> 件以内 <el-input v-model="feeModel.sonFee"></el-input>元，每增加 <el-input v-model="feeModel.sonIncrease"></el-input> 件，增加运费 <el-input v-model="feeModel.sonIncreaseFee"></el-input> 元。
          </template>

          <template v-if="feeModel.sonType==2">
            <span style="padding-left: 30px">运送到</span>
            <el-select v-model="feeModel.provIds" multiple filterable placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select> 地区：
            <br>
            <el-input style="margin-left: 30px" v-model="feeModel.sonLimit"></el-input> g以内 <el-input v-model="feeModel.sonFee"></el-input>元，每增加 <el-input v-model="feeModel.sonIncrease"></el-input> g，增加运费 <el-input v-model="feeModel.sonIncreaseFee"></el-input> 元。
          </template>
          <div style="text-align: right;padding: 30px 30px 20px 0">
            <el-button type="text" @click="saveModeBtn(feeModel)">保存</el-button>
            <el-button type="text" style="color: red" @click="removeModel(index,feeModel.type)">删除此模板</el-button>
          </div>

        </el-form-item>
      </template>
    </el-form>

  </div>
</template>

<script>
  import selectCity from "../../../static/data/adminjson/selectCity.json"
  import {ajax, postModelList, addPostFeeModel, modifyPostFeeModel, deletePostModel} from "../../data/service";

  export default{
    name: "mallPostage",
    data(){
      return{
        modelList:[],
        options:[],
        value:'',
      }
    },
    mixins:[ajax],
    created(){
      this.getCity();
      this.getList();
    },
    methods:{
      getList(){
        this.doajax(postModelList,{},(resp)=>{
          this.modelList = resp.data;
        })
      },
      saveModeBtn(feeModel){
        if(feeModel.feeId){
          this.doajax(modifyPostFeeModel,feeModel,(resp)=>{
            this.getList();
          });
        }else{
          this.doajax(addPostFeeModel,feeModel,(resp)=>{
            this.getList();
          });
        }
        this.success("保存成功")
      },
      getCity(){
        this.options = selectCity.data;
      },
      byCityBtn(val){
        this.modelList[val].sonType = 1;
        this.addModel();
        this.removeModel(this.modelList.length);
      },
      byCityBtnTwo(val){
        this.modelList[val].sonType = 2 ;
        this.addModel();
        this.removeModel(this.modelList.length);
      },
      addModel(type){
        let model = {};
        model.type = type;
        this.modelList.push(model);
      },
      removeModel(index){
        let feeModel = this.modelList[index];
        if(feeModel!==undefined && feeModel.feeId!==undefined){
          this.doajax(deletePostModel,{feeId:feeModel.feeId},(resp)=>{
            this.getList();
            this.success("保存成功");
          });
        }else{
          this.modelList.splice(index,1);
        }
      }
    }
  }
</script>

<style>
  .oneInputW{
    background: #F2F2F2;
    width: 1000px;
  }

  .oneInputW .el-input{
    width: 100px;
  }

  .el-select>.el-input{
   width: 500px;
    margin: 10px 0;
  }
</style>
